<template>
    <div>
        <!--卡片-->
        <el-card class="box-card">
            <!--定义面包屑-->
            <el-breadcrumb separator=">">
                <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
                <el-breadcrumb-item>系统管理</el-breadcrumb-item>
                <el-breadcrumb-item>租赁管理</el-breadcrumb-item>
            </el-breadcrumb>
            <el-divider></el-divider>
            <!--表单的搜索框杀杀杀
                 :model="双向绑定表单的数据"
            -->
            <el-form :inline="true" :model="carFormData"  class="demo-form-inline">
                <el-form-item >
                    <el-input v-model="carFormData.typeName" placeholder="请输入汽车类型"></el-input>
                </el-form-item>

                <el-form-item>
                    <el-button type="primary" @click="chaxun">查询</el-button>
                    <el-button type="primary" @click="insert">添加</el-button>
                </el-form-item>
            </el-form>

            <!--数据表格-->
            <template>
                <el-table
                        :data="tableData"
                        :border="true"
                        stripe
                        style="width: 100%">
                    <el-table-column
                            prop="id"
                            label="编号"
                            width="180">
                    </el-table-column>
                    <el-table-column
                            prop="typeName"
                            label="车类型"
                            width="180">
                    </el-table-column>

                    <el-table-column
                            prop="startPrice"
                            label="起步价">
                    </el-table-column>
                    <el-table-column
                            prop="addtime"
                            label="添加时间">
                    </el-table-column>

                    <el-table-column
                            prop="url"
                            label="路由">
                    </el-table-column>

                    <el-table-column label="操作">
                        <template slot-scope="scope">
                            <el-button type="primary" icon="el-icon-edit" circle @click="selectById(scope.row)"></el-button>
                            <el-button type="danger" icon="el-icon-delete" circle @click="deleteById(scope.row.id)"></el-button>
                            <!--                            <el-button type="warning" icon="el-icon-star-off" circle @click="showUserRole(scope.row.userId)"></el-button>-->
                        </template>
                    </el-table-column>
                </el-table>

                <!--                分页-->
                <el-pagination
                        @size-change="handleSizeChange"
                        @current-change="handleCurrentChange"
                        :current-page="currentPage"
                        :page-sizes="[5, 10, 15, 20]"
                        :page-size="pageSize"
                        layout="total, sizes, prev, pager, next, jumper"
                        :total="total">
                </el-pagination>
            </template>
        </el-card>
        <!--        提示框-->
        <el-dialog
                title="提示"
                :visible.sync="editDialogVisible"
                width="30%"
                :before-close="handleClose"
        >

            <!--            model:表单数据-->
            <el-form  label-width="80px" :model="carForm" :rules="insertFormRules"  ref="insertFormRef">
                <el-form-item label="id" style="display: none">
                    <el-input v-model="carForm.id" ></el-input>
                </el-form-item>

                <el-form-item label="类型" >
                    <el-select v-model="carForm.typeName" placeholder="请选择配置">
                        <el-option
                                v-for="item in typeArray"
                                :key="item.id"
                                :label="item.typeName"
                                :value="item.id"
                        >
                        </el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label="起步价" prop="startPrice">
                    <el-input v-model="carForm.startPrice"></el-input>
                </el-form-item>

                <el-form-item label="路由" prop="url">
                    <el-input v-model="carForm.url"></el-input>
                </el-form-item>
            </el-form>

            <span slot="footer" class="dialog-footer">
                <el-button @click="handleClose">取 消</el-button>
                <el-button type="primary" @click="formSubmit">确 定</el-button>
              </span>
        </el-dialog>
    </div>
</template>
<script>
    export default {
        name: "Allcar",
        data(){
            return {
                insertFormRules:{
                    startPrice:[
                        { required: true, message: '请输入起步价格', trigger: 'blur' },
                        // { type: 'number', message: '价格必须为数字值',trigger: 'change'},
                        {pattern: /^[+]{0,1}(\d+)$|^[+]{0,1}(\d+\.\d+)$/, message: '请输入正确的价格', trigger: 'change'},
                        // {pattern:  /^[0-9]*[1-9][0-9]*$/, message: '请输入正确的价格', trigger: 'change'}
                    ],
                    url:[
                        { required: true, message: '请输入路由名称', trigger: 'blur' }
                    ]
                },
                editDialogVisible:false,
                //条件搜索对象
                carFormData:{},
                //表格数据
                tableData:[],
                //每页显示的条数
                pageSize: 5,
                //当前的显示的页码
                currentPage:1,
                //总条数
                total:0,
                //添加表:
                carForm:{},
                typeArray:[],
            }
        },
        created() {
            this.selectAllCar();
            this.allselectlist(); //全部下拉框
        },
        methods:{
            insert(){
                this.editDialogVisible=true;
                this.carForm={};
            },
            deleteById(id){
                var that=this;
                this.$http.get(`lease/carType/deleteCarType/${id}`).then(function (returnResult) {
                    that.$message.success("删除成功");
                    that.selectAllCar();
                    // location.reload();
                })
                this.currentPage=1;
            },
            selectById(row){
                var that=this;
                //console.log("广东我AUS")
                console.log(row);
                that.editDialogVisible=true;

                var idd = row.id;
                this.$http.get(`/lease/carType/selectCarTypeById/${idd}`).then(function (r) {
                    that.carForm=r.data.result.tbCarType;
                    console.log(r)
                })
                // this.$http.get(`lease/car/selectCarById/${id}`).then(function (returnResult) {
                //     //that.editDialogVisible=true;
                //     // console.log(returnResult);
                //     that.carForm=returnResult.data.result.tbCar;
                //
                // })
            },
            formSubmit(){
                var that=this;
                if(this.carForm.id!=null){
                    this.$http.put("lease/carType/updateCarTypeById",this.carForm).then(function (returnResult) {
                        that.editDialogVisible=false;
                        that.carForm={};
                        that.selectAllCar();
                    })
                }else {
                    this.$http.put("lease/carType/insertCarType", this.carForm).then(function (returnResult) {
                        that.editDialogVisible = false;
                        that.selectAllCar();
                    })
                }
            },
            allselectlist(){
                var that=this;
                // this.$http.get("lease/carConfig/selectAllCarConfig").then(function (returnResult) {
                //     that.configArray=returnResult.data.result.AlltbConfigs;
                // })
                // this.$http.get("/lease/carBrand/selectAllCarBrand").then(function (returnResult) {
                //     that.brandsArray=returnResult.data.result.AlltbBrands;
                // })
                this.$http.get("/lease/carType/selectAllCarType").then(function (returnResult) {
                    that.typeArray=returnResult.data.result.AlltbTypes;
                })
                // this.$http.get("/lease/carShop/selectAllCarShop").then(function (returnResult) {
                //     that.shopArray=returnResult.data.result.AlltbShops;
                // })
            },
            chaxun(){
                this.selectAllCar();
            },
            selectAllCar(){
                var that=this;
                this.$http.post(`/lease/carType/initCarTypeTable/${this.currentPage}/${this.pageSize}`,this.carFormData).then(function (returnResult) {
                    console.log(returnResult)
                    that.tableData=returnResult.data.result.records;
                    that.total=returnResult.data.result.total;
                })
            },
            // 改变条数
            handleSizeChange(v){
                this.pageSize=v;
                this.selectAllCar();
            },
            //改变页码
            handleCurrentChange(v){
                this.currentPage=v;
                this.selectAllCar();
            },
            handleClose()
            {
                this.editDialogVisible=false;
                this.$refs.insertFormRef.resetFields();
            },

        }
    }
</script>

